﻿<?php
session_start();
//error_reporting(0);
require_once("../common/config.php");
$conn = new connectDb();

//获得用户输入的食品信息
function getUserId($login_mail)
{
	$query = mysql_query("SELECT `id` FROM `food_user_basic` WHERE `user_mail` = '".$login_mail."'") or die (mysql_error());
	$row = mysql_fetch_array($query);
	$user_id = $row['id'];
	
	return $user_id;
}

$user_id = getUserId($_SESSION['login_mail']);
$eatWhat = $_POST['eatWhat'];
$eatHowMany = $_POST['eatHowMany'] / 1000;

$query1 = mysql_query("SELECT `eathowmany` FROM `food_user_data` WHERE `eatWhat` = '".$eatWhat."' AND `history_data` = 0 AND `user_id` = '".$user_id."'") or die(mysql_error());
$num1 = mysql_num_rows($query1);
if($num1 == 0):
	mysql_query("INSERT INTO `food_user_data` (`id`, `user_id`, `eatWhat`, `eatHowMany`) VALUES (NULL, '".$user_id."', '".$eatWhat ."', '".$eatHowMany."')") or die (mysql_error());
else:
	$row1 = mysql_fetch_array($query1);
	$formEatHowMany = $row1['eathowmany'];
	$eatZL = $formEatHowMany + $eatHowMany;
	mysql_query("UPDATE `food_user_data` SET `eathowmany` = '".$eatZL."' , `create_time` = now() WHERE `eatWhat` = '".$eatWhat."'") or die(mysql_error());
endif;

	echo "
	<table class='mt15 ml20 score_foodTable tc'>
		<tr class='cf f18 zy_parent'>
			<th class='score_img score_thBgPos tc'>时间</th>
			<th class='score_img score_thBgPos tc'>吃了啥</th>
			<th class='score_img score_thBgPos tc'>吃了多少(kg)</th>
			<th class='score_img score_thBgPos2 tc'>修改</th>
		</tr>
	";


//当前时间
//mysql_query("UPDATE `food_user_basic` SET `create_time` = now() WHERE `id` = '$user_id'") or die(mysql_error());
$sqltime = mysql_query("SELECT `create_time` FROM `food_user_basic` WHERE `id` = '$user_id'") or die(mysql_error());
$rowtime = mysql_fetch_array($sqltime,MYSQL_ASSOC);
$todayDate = substr($rowtime['create_time'],0,10);

$query2 = mysql_query("SELECT * FROM `food_user_data` WHERE `user_id` =  '".$user_id."' AND `history_data` = 0 ORDER BY `create_time` ASC");
while($row2 = mysql_fetch_array($query2,MYSQL_ASSOC))
{
	$createTime = substr($row2['create_time'],0,10);
	if($todayDate == $createTime)
	{
		$create_time = substr($row2['create_time'],10,6);
		$eatWhat1 = $row2['eatwhat'];
		$eatHowMany1 = $row2['eathowmany'];
		$data_id = $row2['id'];
		
		echo "
			<tr class=\"score_foodTbody\">
				<td>$create_time</td>
				<td>$eatWhat1</td>
				<td>$eatHowMany1</td>
				<td>
					<a href=\"\" class=\"score_delete\">删除该项目</a>
					<input type=\"hidden\" class=\"hidden_data_id\" value=\"$data_id\">
				</td>
			</tr>
		";
		
	}
	
}
	echo "</table>
		<input type='submit' value='饮食打分' class='mt20 mb30 score_img png score_eatSub f18 jquery_score_button cf ml20' id='score_sub'/>
	";
	



?>